package com.qianfeng.smsplatform.webmaster.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.smsplatform.common.constants.CacheConstants;
import com.qianfeng.smsplatform.webmaster.dao.TBlackListMapper;
import com.qianfeng.smsplatform.webmaster.dto.DataGridResult;
import com.qianfeng.smsplatform.webmaster.dto.QueryDTO;
import com.qianfeng.smsplatform.webmaster.mq.PushBlackNumChangeService;
import com.qianfeng.smsplatform.webmaster.pojo.TBlackList;
import com.qianfeng.smsplatform.webmaster.pojo.TBlackListExample;
import com.qianfeng.smsplatform.webmaster.service.BlackService;
import com.qianfeng.smsplatform.webmaster.service.api.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BlackServiceImpl implements BlackService {

    @Autowired
    private TBlackListMapper tBlackListMapper;

    @Autowired
    private CacheService cacheService;

    private PushBlackNumChangeService pushBlackNumChangeService;
    @Autowired
    public void setPushBlackNumChangeService(PushBlackNumChangeService pushBlackNumChangeService) {
        this.pushBlackNumChangeService = pushBlackNumChangeService;
    }

    @Override
    public int addBlack(TBlackList tBlackList) {
        //  cacheService.saveCache("BLACK:"+tBlackList.getMobile(),"1");
        cacheService.add2Set(CacheConstants.CACHE_BLACK_KEY, Arrays.asList(tBlackList.getMobile()));//保存到redis
        //发送消息通知策略模块
        MessageChannel messageChannel = pushBlackNumChangeService.pushBlackNumChangeChannel();
        messageChannel.send(new GenericMessage<>("a"));//发送消息, 消息的内容无所谓 只要不是空的就可以
        return tBlackListMapper.insertSelective(tBlackList);
    }

    @Override
    public int delBlack(Long id) {
        TBlackList tBlackList = findById(id);
        //     cacheService.del("BLACK:"+tBlackList.getMobile());
        //TODO 从redis 的set中移除当前数据
        MessageChannel messageChannel = pushBlackNumChangeService.pushBlackNumChangeChannel();
        messageChannel.send(new GenericMessage<>("a"));//发送消息, 消息的内容无所谓 只要不是空的就可以
        return tBlackListMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateBlack(TBlackList tBlackList) {
        //TODO 先根据当前的id查询到原始的数据,找到原始的号码
        int i = tBlackListMapper.updateByPrimaryKey(tBlackList);
        if (i > 0) {
            //TODO 从redis中删除原始号码
            //将新数据添加到redis中
            cacheService.saveCache("BLACK:" + tBlackList.getMobile(), "1");
        }
        return i;
    }

    @Override
    public TBlackList findById(Long id) {
        return tBlackListMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TBlackList> findAll() {
        return tBlackListMapper.selectByExample(null);
    }

    @Override
    public DataGridResult findByPage(QueryDTO queryDTO) {
        PageHelper.offsetPage(queryDTO.getOffset(), queryDTO.getLimit());
        TBlackListExample example = new TBlackListExample();
        String sort = queryDTO.getSort();
        if (!StringUtils.isEmpty(sort)) {
            example.setOrderByClause("id");
        }
        List<TBlackList> tBlackLists = tBlackListMapper.selectByExample(example);
        PageInfo<TBlackList> info = new PageInfo<>(tBlackLists);
        long total = info.getTotal();
        DataGridResult result = new DataGridResult(total, tBlackLists);
        return result;
    }

    @Override
    public void syncBlackToredis() {
        TBlackListExample example = new TBlackListExample();
        List<TBlackList> tBlackLists = tBlackListMapper.selectByExample(example);
//        HashMap<Object, Object> hashMap = new HashMap<>();
//        for (TBlackList tBlackList : tBlackLists) {
//            hashMap.put("BLACK:" + tBlackList.getMobile(), "1");
//        }
//        Map<String, Object> stringMap = tBlackLists.stream().map(tBlackList -> "BLACK:" + tBlackList.getMobile()).collect(Collectors.toMap(String -> String, String::length));
//        System.err.println(stringMap);
//        cacheService.pipelineOps(stringMap);
        //将所有的黑名单保存到redis的集合中
        List<String> blackNums = tBlackLists.stream().map(TBlackList::getMobile).collect(Collectors.toList());//获取到所有的黑名单
        cacheService.add2Set(CacheConstants.CACHE_BLACK_KEY, blackNums);

        MessageChannel messageChannel = pushBlackNumChangeService.pushBlackNumChangeChannel();
        messageChannel.send(new GenericMessage<>("a"));//发送消息, 消息的内容无所谓 只要不是空的就可以
    }
}
